package com.odontosis.view.servico;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;

import com.odontosis.acesso.filtro.PermissaoAcesso;
import com.odontosis.entidade.GrupoUsuario;
import com.odontosis.entidade.Servico;
import com.odontosis.entidade.TipoServico;
import com.odontosis.service.ServicoService;
import com.odontosis.util.StringUtilsOdontosis;
import com.odontosis.view.OdontosisAction;
import com.odontosis.view.cadastro.paciente.FormCadastroPaciente;

public class AcaoListaServico extends OdontosisAction<ServicoService> {
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		FormListaServico formulario = (FormListaServico) form;
		if(StringUtils.isEmpty(formulario.getMetodo()))
		{
			return onIniciar(mapping, form, request, response);
		}else if(formulario.getMetodo().equals("deletar")){
			return onDeletar(mapping, form, request, response);
		}
		return onIniciar(mapping, form, request, response);
	}
	
	
	/**
	 * Carrega radios de tipos de servio.
	 * @param request Requisio HTTP.
	 */
	private void carregarRadiosTiposServico(HttpServletRequest request) {
		TipoServico[] tiposServico = TipoServico.values();
		List<LabelValueBean> valueBeans = new ArrayList<LabelValueBean>();
		for (TipoServico tipoServico : tiposServico) {
			valueBeans.add(new LabelValueBean(
				tipoServico.getDescricao(),
				String.valueOf(tipoServico.getId())));
		}
		request.getSession().setAttribute("tiposServico", valueBeans);
	}
	
	public ActionForward onIniciar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		FormListaServico formulario = (FormListaServico) form;
		TipoServico tipoServico = null; 
		if(!StringUtilsOdontosis.isVazia(formulario.getTipoServico())){
			tipoServico = TipoServico.recuperarPorId(new Integer(formulario.getTipoServico()));
		}
		boolean orcamento = false;
		String path = request.getServletPath();
		if(path.indexOf("listaOrcamento.do") > 0){
			orcamento = true;
			formulario.setMetodo("orcamento");
		}
		
		Collection<Servico> colecao = service.buscarServicosPorPacienteTipo(formulario.getNomePaciente(), tipoServico,formulario.getNumeroPasta(), orcamento);
		
		request.setAttribute("colecao", colecao);
		return mapping.getInputForward();
	}
	
	public ActionForward onDeletar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		FormListaServico formulario = (FormListaServico) form;
		//excluir serviço e parcelas
		if(PermissaoAcesso.verificarAcesso(mapping, form, request, response, GrupoUsuario.ADMIN, GrupoUsuario.SECRETARIA)){
			service.excluirLogicamente(service.load(new Long(formulario.getExcluido())));
			formulario.setMetodo(null);
		}
		Collection<String> sucesso = new ArrayList<String>();
		sucesso.add("Exclusão realizada com sucesso.");
		request.setAttribute("sucesso", sucesso);
		request.setAttribute("mensagens", null);
		
		return onIniciar(mapping, form, request, response);
	}
	
}
